import { CreepsConfig,FlagConfig,RoomTask } from './game/global'
import { CreepFactory } from './creep/creepFactory';

// 扩展
import './prototypes/room.prototype';
import './prototypes/creep.prototype';



// 房间任务
for (const roomName in Game.rooms) {
  const room = Game.rooms[roomName];
  const controllerLevel = room.controller?.level;
  room.memory.spawnAllow = true; // 默认允许生成
  if (controllerLevel) {
    // 根据配置生成对应的虫子
    CreepFactory.generateCreep(room, controllerLevel,CreepsConfig);
    CreepFactory.deleteInvalidMemory();
  }
  for(const task of RoomTask){
    task.run(room);
  }
}

const filteredCreeps = Object.values(Game.creeps).filter((creep) => creep.name === "");

// 虫子任务
for(const creepName in Game.creeps){
    const creep = Game.creeps[creepName];
    creep.run();
}

// 旗帜任务
for (const flagName in Game.flags) {
  const flag = Game.flags[flagName];
  let name = flag.name;
  let param:string | undefined = undefined;

  if(name.indexOf(":")>-1){
    param = name.split(":")[1];
    name = name.split(":")[0];
  }

  for (const { flagName, taskLogic } of FlagConfig) {
    if (name === flagName) {
      const task:IFlagTask = new taskLogic();
      task.run(flag,param);
    }
  }
}
